package cn.tedu._03weibo.controller;

import cn.tedu._03weibo.base.response.JsonResult;
import cn.tedu._03weibo.base.response.StatusCode;
import cn.tedu._03weibo.mapper.UserMapper;
import cn.tedu._03weibo.pojo.dto.UserLoginParam;
import cn.tedu._03weibo.pojo.dto.UserRegParam;
import cn.tedu._03weibo.pojo.entity.User;
import cn.tedu._03weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@Tag(name = "用户模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Operation(summary = "用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult userReg(@Validated @RequestBody UserRegParam userRegParam){
        log.debug("开始处理【用户注册】的请求，参数：{}",userRegParam);
        String usernameParam = userRegParam.getUsername();
        String username = userMapper.selectByUsername(usernameParam);
        if (username != null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        log.debug("用户信息：" + user);
        int result = userMapper.addRegUser(user);
        if (result > 0){
            return JsonResult.ok();
        }
        return JsonResult.fail(StatusCode.OPERATION_FAILED);
    }

    @PostMapping("login")
    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult userLogin(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("开始处理【用户登录】的请求，参数：{}",userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsernameAndPassword(username, password);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }


    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        log.debug("开始处理【获取当前用户】的请求");
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO == null){
            return JsonResult.fail("未登录");
        }
        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @Operation(summary = "用户退出登录")
    @ApiOperationSupport(order = 40)
    public JsonResult logout(HttpSession session){
        log.debug("开始处理【用户退出登录】的请求");
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO == null){
            return JsonResult.fail("当前用户已退出登录");
        }
        session.removeAttribute("user");
        return JsonResult.ok("退出登录成功");
    }

}
